﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 

namespace CochinitaCorte
{
    public partial class MainForm : Form
    {
        private enum AuditType
        { 
           CORTE_FTP = 1,
           CORTE_LOCAL = 2
        }

        private BusinessLayer BL = new BusinessLayer();
        
        public delegate void FormReturn(string Action, string Value);
        
        public MainForm()
        {
            InitializeComponent();
        }

        private bool validaciones()
        {
            //Validaciones de salida
            try
            {               
                 if (franquicias.SelectedItems.Count == 0)
                    throw new Exception("NOSELECCIONADO");
                 else if ((DateTime.Compare(dtStart.Value , dtEnd.Value)) > 0)
                    throw new Exception("FECHAS");
            }
            catch (Exception ex)
            {
                if (ex.Message == "NOSELECCIONADO")
                    MessageBox.Show("No se puede procesar el Corte sin seleccionar una Franquicia", "Seleccione una Franquicia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else if(ex.Message == "FECHAS")
                    MessageBox.Show("La Fecha Inicial no puede ser posterior a la Final.", "Ajuste Las Fechas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            
                return false;
            }

            return true;
        }

    

        private void ProcesarCorte()
        {
            List<string> FranquiciasSeleccionadas = new List<string>();
           
            foreach (string franquicia in franquicias.CheckedItems)
                FranquiciasSeleccionadas.Add(franquicia);

            dtGrid.DataSource = BL.Process(dtStart.Value, dtEnd.Value, FranquiciasSeleccionadas, radioFTP.Checked);

        }
 
        private void franquicias_DoubleClick(object sender, EventArgs e)
        {
            FiscalDataForm fiscalinfo = new FiscalDataForm(ReturnValues, BL.GetFiscalData(franquicias.SelectedItem.ToString()), BL.GetMetas(franquicias.SelectedItem.ToString()));
            fiscalinfo.ShowDialog();
        }
        
        protected void ReturnValues(string Action, string Value)
        {
            BL.RefreshParameters();
                        
            if (Action == "IMPRIMIR")
            {
                if (Value.Equals("CORTE_Z"))
                    BL.PrintCorteZ();
                else if (Value.Equals("CORTE_N"))
                    BL.PrintCorteN();
                else if (Value.Equals("CORTE_C"))
                    BL.PrintCorteC();
                else if (Value.Equals("CORTE_COMPLETE"))
                    BL.PrintCorteComplete();
            }
            else if (Action == "FISCAL_DATA")
            {
           
            }

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
              foreach(string line in (List<string>)BL.GetFranquicias())
                franquicias.Items.Add(line);

              dtStart.Value = DateTime.Now;
              dtEnd.Value = DateTime.Now;
        }
 

        private void salirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void acercaDeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About frmAbout = new About();
            frmAbout.ShowDialog();
        }

        #region CUSTOM
      
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
         {
            SettingsForm printerSelection = new SettingsForm(ReturnValues, BL.GetSettings());
             printerSelection.ShowDialog();
         }

        private void controlDeUsuariosToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UsersManager frmUsers = new UsersManager();
            frmUsers.ShowDialog();
        }

        private void btnPrint_Click_1(object sender, EventArgs e)
        {
            printDialog imprimir = new printDialog(ReturnValues, BL.GetSettings());
            imprimir.ShowDialog();
        }

        private void btnProcess_Click_1(object sender, EventArgs e)
        {
            if (validaciones())
            {
                ProcesarCorte();
                btnPrint.Enabled = true;
            }
        }

        private void Salir_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void configuracionFTPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FtpParametros FTPForm = new FtpParametros();
            FTPForm.ShowDialog();
        }

     
        #endregion

        private void franquicias_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       
    }
}
